{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/00a_RF_tuning_EU.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}18 Dec 2023, 15:15:35
{txt}
{com}. 
{txt}end of do-file

{com}. do "C:\Users\thies\AppData\Local\Temp\STD4cfc_000000.tmp"
{txt}
{com}. set seed 1234
{txt}
{com}. 
. *Prepare data
. use "Dataset.dta", clear
{txt}
{com}. keep if sender=="EU"
{txt}(10,154 observations deleted)

{com}. 
. ** Filter for cases of importance
. keep if pot_sanctioned_countries == 1
{txt}(1,296 observations deleted)

{com}. 
. xtset ccodecow year
{res}
{col 1}{txt:Panel variable: }{res:ccodecow}{txt: (unbalanced)}
{p 1 16 2}{txt:Time variable: }{res:year}{txt:, }{res:{bind:1989}}{txt: to }{res:{bind:2015}}{p_end}
{txt}{col 10}Delta: {res}1 unit
{txt}
{com}. gen ln_oil_gas_value_2014 = ln(oil_gas_value_2014+1)
{txt}(197 missing values generated)

{com}. gen sender_colony=0
{txt}
{com}. replace sender_colony=1 if ht_colonial==2 | ht_colonial==3 | ht_colonial==6 | ht_colonial==7 | ht_colonial==8 | ht_colonial==9 | ht_colonial==10
{txt}(2,539 real changes made)

{com}. 
. gen sender_additional=cond(threatUS==1 | impositionUS == 1, 1, 0)
{txt}
{com}. gen only_threat=cond(threatEU==1 & impositionEU == 0, 1, 0)
{txt}
{com}. 
. gen sender_trade = ln_EU_Trade_Eurostat
{txt}(20 missing values generated)

{com}. gen coup_dummy = coup1
{txt}(5 missing values generated)

{com}. replace coup_dummy = 0 if coup_dummy == 1
{txt}(61 real changes made)

{com}. replace coup_dummy = 1 if coup_dummy == 2
{txt}(45 real changes made)

{com}. 
. * variable which is 1 if a threat or sanction case was ongoing in the dyad
. gen sanction_threat = sanction_dyad
{txt}
{com}. replace sanction_threat = 1 if threat_dyad==1
{txt}(49 real changes made)

{com}. tab sanction_threat

{txt}sanction_th {c |}
       reat {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}      3,299       87.25       87.25
{txt}          1 {c |}{res}        482       12.75      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      3,781      100.00
{txt}
{com}. gen sanction_train= sanction_threat if year < 2009
{txt}(992 missing values generated)

{com}. gen sanction_test= sanction_threat if year >= 2009
{txt}(2,789 missing values generated)

{com}. 
. * lag time-series variables
. sort ccodecow year
{txt}
{com}. by ccodecow: gen l_v2x_polyarchy = v2x_polyarchy[_n-1] if year==year[_n-1]+1
{txt}(240 missing values generated)

{com}. by ccodecow: gen l_gd_ptss = gd_ptss[_n-1] if year==year[_n-1]+1
{txt}(225 missing values generated)

{com}. by ccodecow: gen l_coup_dummy = coup_dummy[_n-1] if year==year[_n-1]+1
{txt}(151 missing values generated)

{com}. by ccodecow: gen l_one_sided_violence = one_sided_violence[_n-1] if year==year[_n-1]+1
{txt}(147 missing values generated)

{com}. by ccodecow: gen l_conflict = conflict[_n-1] if year==year[_n-1]+1
{txt}(147 missing values generated)

{com}. by ccodecow: gen l_mid_terr_integrity = mid_terr_integrity[_n-1] if year==year[_n-1]+1
{txt}(147 missing values generated)

{com}. by ccodecow: gen l_ln_GDPpc_imputed = ln_GDPpc_imputed[_n-1] if year==year[_n-1]+1
{txt}(187 missing values generated)

{com}. by ccodecow: gen l_sender_trade = sender_trade[_n-1] if year==year[_n-1]+1
{txt}(165 missing values generated)

{com}. by ccodecow: gen l_ln_oil_gas_value = ln_oil_gas_value_2014[_n-1] if year==year[_n-1]+1
{txt}(202 missing values generated)

{com}. by ccodecow: gen l_defense_alliance = defense_alliance[_n-1] if year==year[_n-1]+1
{txt}(147 missing values generated)

{com}. 
. * create dummy variables
. tabulate l_gd_ptss, generate (pol_terr)

  {txt}l_gd_ptss {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          1 {c |}{res}        465       13.08       13.08
{txt}          2 {c |}{res}      1,009       28.37       41.45
{txt}          3 {c |}{res}      1,190       33.46       74.92
{txt}          4 {c |}{res}        634       17.83       92.74
{txt}          5 {c |}{res}        258        7.26      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      3,556      100.00
{txt}
{com}. 
. 
. ** Imposition
. * Tuning number of iterations of rf model
. * Randomize data
. gen u=0
{txt}
{com}. replace u=1 if year >= 2009
{txt}(992 real changes made)

{com}. sort u
{txt}
{com}. gen out_of_bag_error1 = .
{txt}(3,781 missing values generated)

{com}. gen training_error = .
{txt}(3,781 missing values generated)

{com}. gen recall_iter = .
{txt}(3,781 missing values generated)

{com}. gen recall_training_sample_iter = .
{txt}(3,781 missing values generated)

{com}. gen iter1 = .
{txt}(3,781 missing values generated)

{com}. local j = 0
{txt}
{com}. 
. forvalues i = 100(200)1500{c -(}
{txt}  2{com}. local j = `j' + 1
{txt}  3{com}. rforest sanction_threat l_v2x_polyarchy pol_terr* l_coup_dummy ///
> l_one_sided_violence l_conflict l_mid_terr_integrity ///
> l_ln_GDPpc_imputed l_sender_trade l_ln_oil_gas_value ///
> sender_colony l_defense_alliance in 1/2789, ///
> type(class) iter(`i') numvars(1)
{txt}  4{com}. replace iter1 = `i' in `j'
{txt}  5{com}. replace out_of_bag_error1 = `e(OOB_Error)' in `j'
{txt}  6{com}. predict p in 2790/3781
{txt}  7{com}. count if p==1 & sanction_test == 1
{txt}  8{com}. replace recall_iter = r(N)/125 in `j'
{txt}  9{com}. predict p2 in 1/2789
{txt} 10{com}. count if p2==1 & sanction_train == 1
{txt} 11{com}. replace recall_training_sample_iter = r(N)/357 in `j'
{txt} 12{com}. replace training_error = `e(error_rate)' in `j'
{txt} 13{com}. drop p p2
{txt} 14{com}. {c )-}
{txt}(1 real change made)
(1 real change made)
  {res}34
{txt}(1 real change made)
  {res}347
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}29
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}28
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}27
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}26
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}26
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}27
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}27
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)

{com}. 
. *Additional tuning based on recall rate, as outcome variable is imbalanced.
. label var recall_iter "Recall Test Sample"
{txt}
{com}. label var recall_training_sample_iter "Recall Training Sample"
{txt}
{com}. scatter recall_iter recall_training_sample_iter iter1
{res}{txt}
{com}. 
. graph export "Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_iterations_Recall_Training_Test.png", as(png) name("Graph") replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_iterations_Recall_Training_Test.png{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_iterations_Recall_Training_Test.png{rm}
saved as
PNG
format
{p_end}

{com}. 
. * Tuning based on Accuracy
. label var out_of_bag_error1 "Out of Bag Error"
{txt}
{com}. label var training_error "Accuracy Test Sample"
{txt}
{com}. scatter out_of_bag_error1 training_error iter1
{res}{txt}
{com}. graph export "Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_iterations_Accuracy.png", as(png) name("Graph") replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_iterations_Accuracy.png{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_iterations_Accuracy.png{rm}
saved as
PNG
format
{p_end}

{com}. * No improvement above 1500 iterations
. 
. drop out_of_bag_error1 training_error recall_iter recall_training_sample_iter
{txt}
{com}. 
. * Tuning number of variables to include in model
. gen out_of_bag_error1 = .
{txt}(3,781 missing values generated)

{com}. gen training_error = .
{txt}(3,781 missing values generated)

{com}. gen nvars = .
{txt}(3,781 missing values generated)

{com}. gen recall_iter = .
{txt}(3,781 missing values generated)

{com}. gen recall_training_sample_iter = .
{txt}(3,781 missing values generated)

{com}. local j = 0
{txt}
{com}. 
. forvalues i = 1(1)15{c -(}
{txt}  2{com}. local j = `j' + 1
{txt}  3{com}. rforest sanction_threat l_v2x_polyarchy pol_terr* l_coup_dummy ///
> l_one_sided_violence l_conflict l_mid_terr_integrity ///
> l_ln_GDPpc_imputed l_sender_trade l_ln_oil_gas_value ///
> sender_colony l_defense_alliance in 1/2789, ///
> type(class) iter(1500) numvars(`i')
{txt}  4{com}. replace nvars = `i' in `j'
{txt}  5{com}. replace out_of_bag_error1 = `e(OOB_Error)' in `j'
{txt}  6{com}. predict p in 2790/3781
{txt}  7{com}. count if p==1 & sanction_test == 1
{txt}  8{com}. replace recall_iter = r(N)/125 in `j'
{txt}  9{com}. predict p2 in 1/2789
{txt} 10{com}. count if p2==1 & sanction_train == 1
{txt} 11{com}. replace recall_training_sample_iter = r(N)/357 in `j'
{txt} 12{com}. replace training_error = `e(error_rate)' in `j'
{txt} 13{com}. drop p p2
{txt} 14{com}. {c )-}
{txt}(1 real change made)
(1 real change made)
  {res}27
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}27
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}28
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}30
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}30
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}30
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}36
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}35
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}38
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}40
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}40
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}42
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}42
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}43
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}45
{txt}(1 real change made)
  {res}348
{txt}(1 real change made)
(1 real change made)

{com}. 
. *Additional tuning based on recall rate, as outcome variable is imbalanced.
. label var recall_iter "Recall Test Sample"
{txt}
{com}. label var recall_training_sample_iter "Recall Training Sample"
{txt}
{com}. scatter recall_iter recall_training_sample_iter nvars
{res}{txt}
{com}. graph export "Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_numvars_Recall_Training_Test.png", as(png) name("Graph") replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_numvars_Recall_Training_Test.png{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_numvars_Recall_Training_Test.png{rm}
saved as
PNG
format
{p_end}

{com}. 
. * Accuracy
. label var out_of_bag_error1 "Out of Bag Error"
{txt}
{com}. label var training_error "Accuracy Test Sample"
{txt}
{com}. scatter out_of_bag_error1 training_error nvars 
{res}{txt}
{com}. graph export "Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_numvars_Accuracy.png", as(png) name("Graph") replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_numvars_Accuracy.png{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
Supplemental_Material\Random_Forest_Setup\RF_EU_tuning_pot_sanctions_numvars_Accuracy.png{rm}
saved as
PNG
format
{p_end}

{com}. * include all vars
. 
. log close
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/00a_RF_tuning_EU.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}18 Dec 2023, 15:31:03
{txt}{.-}
{smcl}
{txt}{sf}{ul off}